package com.yidi.system.repository;

import com.yidi.common.core.Repository.BaseRepository;
import com.yidi.common.core.domain.entity.SysUser;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * @author ：Xn
 * @ClassName: SysUserRepository
 * @date ：2021-10-19 14:43
 * @describe ：用户
 */
@Repository
public interface SysUserRepository extends BaseRepository<SysUser,Long> {

    SysUser findByUserName(String userName);

    @Query(nativeQuery = true, value = "select distinct u.id, u.dept_id as deptId, u.user_name as userName, u.nick_name as nickName, u.email, u.phonenumber, u.status, u.create_time as createTime from sys_user u" +
            " left join sys_dept d on u.dept_id = d.id" +
            " left join sys_user_role ur on u.id = ur.user_id" +
            " left join sys_role r on r.id = ur.role_id" +
            " where u.remove_status = '0' and (r.id != ?1 or r.id IS NULL)" +
            " and u.id not in (select u.id from sys_user u inner join sys_user_role ur on u.id = ur.user_id and ur.role_id = ?1)" +
            " if(?2 <> null, and u.user_name like concat('%', ?2, '%'), '')" +
            " if(?3 <> null, and u.phonenumber like concat('%', ?3, '%'), '')")
    List<Map<String, Object>> selectUnallocatedList(Long roleId, String userName, String phonenumber);

    @Query(nativeQuery = true, value = "select count(1) from sys_user where user_name = ?1 limit 1")
    int checkUserNameUnique(String userName);

    SysUser findByPhonenumber(String phonenumber);

    SysUser findByEmail(String email);
}
